package com.hontech.libratest.conn

import com.hontech.libratest.utils.Frame
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.ReentrantLock

class RespSync {

    private val lock = ReentrantLock()
    private val cond = lock.newCondition()
    private var frame: Frame? = null

    fun clear() {
        frame = null
    }

    fun await(timeout: Long): Frame? {
        lock.lock()
        do {
            if (frame != null) {
                break
            }
            cond.await(timeout, TimeUnit.MILLISECONDS)
        } while (false)
        lock.unlock()
        return frame
    }

    fun signal(frame: Frame) {
        lock.lock()
        this.frame = frame
        cond.signal()
        lock.unlock()
    }
}

